﻿CREATE PROCEDURE [dbo].[yaf_registry_save](
                @Name    NVARCHAR(50),
                @Value   NTEXT  = NULL,
                @BoardID INT  = NULL)
AS
    BEGIN
        IF @BoardID IS NULL
        BEGIN
            IF EXISTS (SELECT 1
                       FROM   yaf_Registry
                       WHERE  Lower(Name) = Lower(@Name))
            UPDATE yaf_Registry
            SET    VALUE = @Value
            WHERE  Lower(Name) = Lower(@Name)
            AND BoardID IS NULL
            ELSE
            BEGIN
                INSERT INTO yaf_Registry
                           (Name,
                            VALUE)
                VALUES     (Lower(@Name),
                            @Value)
            END
        END
        ELSE
        BEGIN
            IF EXISTS (SELECT 1
                       FROM   yaf_Registry
                       WHERE  Lower(Name) = Lower(@Name)
                       AND BoardID = @BoardID)
            UPDATE yaf_Registry
            SET    VALUE = @Value
            WHERE  Lower(Name) = Lower(@Name)
            AND BoardID = @BoardID
            ELSE
            BEGIN
                INSERT INTO yaf_Registry
                           (Name,
                            VALUE,
                            BoardID)
                VALUES     (Lower(@Name),
                            @Value,
                            @BoardID)
            END
        END
    END


